var date_eg = laydate.render({
    elem: '#searchTimeHistory',
    range: '~',
    change: function(value, date, endDate){
        if (!isNull(value)) {
            var strings = value.split(" ~ ");
            if (oneMonthLimit(strings[0], strings[1])) {
                date_eg.hint("请将时间范围控制在30天内");
            }
        }
    }
});

$(function () {
    initChart();
    loadHistoryData();
})

function loadHistoryData() {
    chartNoDataHintHide($('#chart_pie2'));
    chartNoDataHintHide($('#chart_pie1'));
    chartNoDataHintHide($('#chart_line1'));
    chartNoDataHintHide($('#chart_bar1'));
    var dateTime = $('#searchTimeHistory').val();
    if (!isNull(dateTime)) {
        var strings = dateTime.split(" ~ ");
        if (oneMonthLimit(strings[0], strings[1])) {
            showModalHint("fail", "请将时间范围控制在30天内");
            return;
        }
    }
    alarmStatistics();
    alarmHandlingInformation();
    AlarmCauseDistribution();
    historyAlarmDeviceType();
}
/*告警设备类型*/
function historyAlarmDeviceType() {
    getJson("/alarm/getHistoryAlarmDeviceType", {dateStr: $('#searchTimeHistory').val()}, 'GET', function (result) {
        var data = result['payload']['data'];
        if (data.length == 0) {
            chartNoDataHint($('#chart_pie2'));
        }
        var list = new Array();
        var total = result['payload']['count'].length;
        data.map(function (item) {
            var innerData = {};
            innerData.value = item['count'];
            innerData.name = item['typeName'];
            list.push(innerData);
        });
        chart_pie2.setOption({//告警原因分布&告警设备类型
            title: {
                text: total,//这里设置环中心数量
            },
            series: [{
                data: list
            }]
        });
        hideLoading();
    });
}
/*告警原因分布*/
function AlarmCauseDistribution() {
    getJson("/alarm/getAlarmCauseDistribution", {dateStr: $('#searchTimeHistory').val()}, 'GET', function (result) {
        var data = result['payload']['data'];
        if (data.length == 0) {
            chartNoDataHint($('#chart_pie1'));
        }
        var list = new Array();
        var total = Number(0);
        data.map(function (item) {
            var innerData = {};
            total += item['count'];
            innerData.value = item['count'];
            innerData.name = item['content'];
            list.push(innerData);
        });
        chart_pie1.setOption({//告警原因分布&告警设备类型
            title: {
                text: total,//这里设置环中心数量
            },
            series: [{
                data: list
            }]
        });
        hideLoading();
    });
}
/*告警统计*/
function alarmStatistics() {
    getJson("/alarm/getAlarmStatistics", {dateStr:$('#searchTimeHistory').val()}, 'GET', function (result) {
        if (result['code'] == 200) {
            var data = result['payload']['data'];
            if (data.length == 0) {
                chartNoDataHint($('#chart_line1'));
            }
            var xArr = new Array();
            var yArr = new Array();
            data.map(function (item) {
                xArr.push(item['time']);
                yArr.push(item['count']);
            });
            chart_line1.setOption({ //告警统计
                xAxis: {
                    data: xArr,
                },
                series: [{
                    data: yArr,
                }]
            })
        }
    })
}
/*告警处理信息*/
function alarmHandlingInformation() {
    getJson("/alarm/getAlarmHandlingInformation", {dateStr: $('#searchTimeHistory').val()}, 'GET', function (result) {
        if (result['code'] == 200) {
            var data = result['payload']['data'];
            if (data.length == 0) {
                chartNoDataHint($('#chart_bar1'));
            }
            var xArr = new Array();
            /*以恢复*/
            var restored = new Array();
            /*未恢复*/
            var notRecovered = new Array();
            /*未处理*/
            var untreated = new Array();
            /*总数*/
            var count = new Array();
            data.map(function (item) {
                xArr.push(item['dataTime']);
                restored.push(item['restored']);
                notRecovered.push(item['notRecovered']);
                untreated.push(item['untreated']);
                count.push(item['restored'] + item['notRecovered'] + item['untreated']);
            });
            chart_bar1.setOption({ //告警处理信息
                xAxis: {
                    data: xArr,
                },
                series: [{
                    data: count//总数量需计算
                }, {
                    data: restored,
                }, {
                    data: notRecovered,
                }, {
                    data: untreated,
                }]
            });
        }
    })
}
function initChart() {
    chart_line1.setOption(option_line1);
    chart_bar1.setOption(option_bar1);
    chart_pie1.setOption(option_pie);
    chart_pie2.setOption(option_pie);
    chart_pie2.setOption({
        title: {
            text: '100',//这里设置环中心数量
            subtext: '总设备',
            left: 303
        },
        legend: {
            left: 630
        },
        series: {
            center: ['310', '50%'],
            data: [],
            label: {
                show: false
            },
            labelLine: {
                show: false
            },
            emphasis: {
                label: {
                    show: false
                },
                labelLine: {
                    show: false
                }
            }
        }
    });
}

var chart_line1 = echarts.init(document.getElementById('chart_line1'));//告警统计
var chart_bar1 = echarts.init(document.getElementById('chart_bar1'));//告警处理信息
var chart_pie1 = echarts.init(document.getElementById('chart_pie1'));//告警原因分布
var chart_pie2 = echarts.init(document.getElementById('chart_pie2'));//告警设备类型
var option_line1 = { //告警统计
    color: ['#FF7931'],
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'line',
            lineStyle: {
                type: 'dashed',
                color: '#D1D1D1'
            }
        },
        backgroundColor: 'rgba(39, 71, 105, 0.7)'
    },
    grid: {
        top: 48,
        left: 15,
        right: 18,
        bottom: 0,
        containLabel: true
    },
    xAxis: {
        data: increaseArr(24, 1),
        type: 'category',
        axisLine: {
            lineStyle: {
                color: 'rgba(209, 230, 249, 0.7)'
            }
        },
        axisLabel: {
            color: 'rgba(209, 230, 249, 0.7)'
        },
        axisTick: {
            lineStyle: {
                color: 'rgba(209, 230, 249, 0.7)'
            }
        },
        splitLine: {
            show: false
        }
    },
    yAxis: {
        type: 'value',
        name: '次',
        nameTextStyle: {
            color: '#fff'
        },
        axisLine: {
            show: false
        },
        axisLabel: {
            color: '#fff'
        },
        axisTick: {
            show: false
        },
        splitLine: {
            lineStyle: {
                color: ['rgba(22, 126, 226, 0.2)'],
                type: 'dashed'
            }
        }
    },
    series: [{
        name: '告警数量',
        type: 'line',
        showSymbol: false,
        data: randomFun(24, 0, 100, 0),
        areaStyle: {
            color: new echarts.graphic.LinearGradient(
                0, 0, 0, 1,
                [
                    {offset: 0, color: 'rgba(255,121,49,0.6)'},
                    {offset: 1, color: 'rgba(255,121,49,0.3)'}
                ]
            )
        }
    }]
};
var option_bar1 = { //告警处理信息
    color: ['rgba(59, 85, 159, 0.3)', '#0096FF', '#FAD488', '#F95959'],
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'shadow',
            shadowStyle: {
                color: 'rgba(59, 85, 159, 0.3)'
            }
        },
        backgroundColor: 'rgba(39, 71, 105, 0.7)'
    },
    grid: {
        top: 48,
        left: 15,
        right: 18,
        bottom: 0,
        containLabel: true
    },
    xAxis: {
        data: increaseArr(24, 1),
        type: 'category',
        axisLine: {
            lineStyle: {
                color: 'rgba(209, 230, 249, 0.7)'
            }
        },
        axisLabel: {
            color: 'rgba(209, 230, 249, 0.7)'
        },
        axisTick: {
            lineStyle: {
                color: 'rgba(209, 230, 249, 0.7)'
            }
        },
        splitLine: {
            show: false
        }
    },
    yAxis: {
        type: 'value',
        name: '次',
        nameTextStyle: {
            color: '#fff'
        },
        axisLine: {
            show: false
        },
        axisLabel: {
            color: '#fff'
        },
        axisTick: {
            show: false
        },
        splitLine: {
            lineStyle: {
                color: ['rgba(22, 126, 226, 0.2)'],
                type: 'dashed'
            }
        }
    },
    series: [{
        name: '报警数量',
        type: 'bar',
        barGap: '-100%',
        data: randomFun(24, 0, 100, 0)//总数量需计算
    }, {
        name: '已恢复',
        type: 'bar',
        data: randomFun(24, 0, 100, 0),
        stack: '报警数量'
    }, {
        name: '未恢复',
        type: 'bar',
        data: randomFun(24, 0, 100, 0),
        stack: '报警数量'
    }, {
        name: '未处理',
        type: 'bar',
        data: randomFun(24, 0, 100, 0),
        stack: '报警数量'
    }]
};
var option_pie = {//告警原因分布&告警设备类型
    title: {
        text: '100',//这里设置环中心数量
        left: '277',
        top: '160',
        textStyle: {
            color: '#00E3D2',
            fontSize: 32,
            fontFamily: 'DIN-Bold'
        },
        subtext: '总告警',
        subtextStyle: {
            color: '#fff',
            fontSize: 18,
            fontWeight: 400,
        },
        itemGap: 16,
        textAlign: 'center'
    },
    color: ['#F8DD83', '#718BF3', '#FF7931', '#5DDF91', '#0096FF'],
    tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b} : {c} ({d}%)',
        backgroundColor: 'rgba(39, 71, 105, 0.7)'
    },
    legend: {
        type: 'scroll',
        pageIconInactiveColor: '#2f4554',
        pageIconColor: '#0056d4',
        pageIconSize: 14,
        pageTextStyle: {
            color: '#aaa'
        },
        left: 596,
        top: 'middle',
        icon: 'rect',
        orient: 'vertical',
        itemHeight: 14,
        itemWidth: 14,
        itemGap: 10,
        textStyle: {
            color: '#83D0EF',
            fontSize: 14,
            padding: [0, 5]
        }
    },
    series: [{
        name: '告警数量',
        type: 'pie',
        radius: [86, 120],
        center: ['284', '50%'],
        label: {
            formatter: '{white|{b}:}{c}',
            lineHeight: 20,
            fontSize: 14,
            rich: {
                white: {
                    color: '#fff',
                    fontSize: 14
                }
            },
            distanceToLabelLine: 10
        },
        labelLine: {
            length: 20,
            length2: 0
        },
        data: []
    }]
};